package cn.com.bluemoon.daps.common.mp;

import cn.com.bluemoon.daps.common.domain.ResultBean;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;

/**
 * common return
 *
 * @param <T>
 * @author xuxueli 2015-12-4 16:32:31
 * @author Jarod Kong Deprecated new 构造器，转为静态获取，增加分页，增加addMoreData,增加泛型
 * @author Jarod Kong 增加cn.com.bluemoon.dap.common.ResultBean#of(cn.com.bluemoon.dap.common.auth.FeignApiRespBean)
 * 用于feign调用返回的数据，转为ResultBean
 */
public class MpResultBean<T> extends ResultBean<T> implements Serializable {

    public MpResultBean(int code, String msg, T content, boolean isPageContent) {
        super(code, msg, content, isPageContent);
    }

    public static <T extends Serializable> ResultBean<Object> toAntDvPage(T content) {
        if (content instanceof IPage) {
            AntDvPage<T> dvPage = new AntDvPage<>((IPage<T>) content);
            return ResultBean.page(dvPage);
        }
        return ResultBean.ok(content);
    }

    /**
     * 针对分页数据为空 导致前端获取基本属性失败的问题
     * 比如{@link AntDvPage#getTotal()} 等
     * 非分页对象，返回原值
     *
     * @return
     */
    public ResultBean<?> orElseEmptyPage() {
        if (this.isPageContent() && this.getContent() == null) {
            return new MpResultBean<>(this.getCode(), this.getMsg(), AntDvPage.empty(), this.isPageContent());
        }
        return this;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this,  ToStringStyle.MULTI_LINE_STYLE);
    }
}
